/**
 * Created by Administrator on 2017/9/3.
 */
/**
 * 聊天区域Dom层管理
 */
define(function (require) {
    function execute() {


        var chatHandler = require("chatHandler"),
            messageBlockDiv = document.getElementById("message-block"),
            ws = require('webSocketChannel');

        chatHandler.on("send-message", function (data) {
            var personMessage = document.createElement("DIV");

            personMessage.classList.add("person-message");
            personMessage.innerHTML = '<div class="person-message__info">' +
                '<img src="image/portrait.png" class="portrait">' +
                '<span class="name">' + data.name + '</span></div>' +
                '<div class="person-message__content">' + data.data + '</div>';

            messageBlockDiv.appendChild(personMessage);

        });
        chatHandler.on("chat-open", function () {
            messageBlockDiv.innerHTML +=
                '<div class="system-message">' +
                '<div class="system-message__content">' +
                '<p>已关闭实时讨论</p>' +
                '<p>请在上方开启</p>' +
                '</div>' +
                '</div>';
        });
        chatHandler.on("chat-closed", function () {
            console.log("Im here")
            messageBlockDiv.innerHTML +=
                '<div class="system-message">' +
                '<div class="system-message__content">' +
                '<p>已开启实时讨论</p>' +
                '</div>' +
                '</div>';
        });

        var move = new (function () {
            var offset;
            var watchingDelay = 2000,
                timeStart = new Date(),
                stopWatching = false;
            this.moveToEnd = function () {
                offset = messageBlockDiv.scrollHeight - messageBlockDiv.offsetHeight;
                messageBlockDiv.scrollTop = offset;
            };

            messageBlockDiv.addEventListener("scroll", scrollWatcher(500));

            // 更新offset
            setInterval(function () {
                if (stopWatching) return;
                move.moveToEnd();
            }, 500);


            function scrollWatcher(delay) {

                var lastTime = new Date();

                return function (e) {
                    if (messageBlockDiv.scrollHeight-messageBlockDiv.scrollTop-messageBlockDiv.offsetHeight>20){
                        stopWatching = true;
                    }else if (new Date()-lastTime>delay){
                        stopWatching = false;
                    }
                }
            }
        })();
        var sendDom = document.getElementById("pre-sending-area__button"),
            inputDom = document.getElementById("pre-sending-area__text");
        sendDom.addEventListener("click",function () {
            if (inputDom.value === ''){
                alert("发送消息内容不能为空")
            }else {
                sendDom.classList.add("disabled");
                chatHandler.send({
                    data:inputDom.value
                },function () {
                    sendDom.classList.remove("disabled");
                    inputDom.value = '';
                },function () {
                    alert("发送失败");
                    sendDom.classList.remove("disabled");
                })
            }
        })


    }

    return {
        execute: execute
    }
})